package com.ydl.learning.flink.demo.window

import com.ydl.learning.flink.demo.Utils
import com.ydl.learning.flink.demo.myFunction.PrintFunction
import org.apache.flink.api.scala._
import org.apache.flink.streaming.api.windowing.time.Time

/**
 * 滑动窗口demo
 * 滑动窗口的数据会重复，使用场景一般是那种每隔多长时间统计下最近多长时间的数据的需求
 * 比方每隔5分钟，统计最近一小时的网站pv
 *
 * @author ydl
 * @since 2020/11/25
 */
object SlideWindowDemo extends Utils with App {
  val stream = getSocketTextStream(9999)
  val data = stream._2.map(new PrintFunction).flatMap(_.split(" ")).map((_, 1))
  data.keyBy(0).timeWindow(Time.seconds(10), Time.seconds(5))
    .sum(1)
    .print()
  stream._1.execute("SlideWindowDemo")
}
